<template>
  <PageWrapper fixedHeight contentFullHeight>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> {{ t('新增') }} </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex == 'state'">
          <a-switch v-model:checked="record.state" :checkedValue="1" :unCheckedValue="0" disabled />
        </template>
        <template v-if="column.dataIndex == 'isStandard'">
          <span>{{ record.isStandard ? '是' : '否' }}</span>
        </template>
        <template v-if="column.dataIndex == 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                onClick: handleDelete.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <ConfigModal @register="registerModal" @success="reload" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { BasicTable, useTable, TableAction, BasicColumn, FormSchema } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { PageWrapper } from '/@/components/Page';
  import { getConversionPageList, deleteConversionList } from '/@/api/erp/unit/conversion';
  import { getUnitTypeList } from '/@/api/erp/unit/type';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import ConfigModal from './ConfigModal.vue';
  const { t } = useI18n();
  const columns: BasicColumn[] = [
    {
      title: '单位类型',
      dataIndex: 'unitTypeName',
      sorter: true,
    },
    {
      title: '基准单位',
      dataIndex: 'standardName',
      sorter: true,
    },
    {
      title: '换算关系',
      dataIndex: 'quantity',
      sorter: true,
    },
    {
      title: '状态',
      dataIndex: 'state',
      sorter: true,
    },
  ];

  const searchFormSchema: FormSchema[] = [
    {
      field: 'unitTypeId',
      label: '单位类型',
      component: 'ApiSelect',
      colProps: { span: 8 },
      componentProps: {
        placeholder: '请选择单位类型',
        api: getUnitTypeList,
        labelField: 'name',
        valueField: 'id',
        getPopupContainer: () => document.body,
      },
    },
  ];
  const existUniTypeIdList = ref<string[]>([]);
  const { notification } = useMessage();
  const [registerModal, { openModal }] = useModal();

  const [registerTable, { reload }] = useTable({
    title: '单位换算配置',
    api: getConversionPageList,
    rowKey: 'id',
    columns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
    },
    afterFetch: (data) => {
      existUniTypeIdList.value = [...new Set(data?.map((x) => x.unitTypeId))] as string[];
    },
    useSearchForm: true,
    showTableSetting: true,
    striped: false,
    actionColumn: {
      width: 80,
      title: t('操作'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
  });

  const handleCreate = () => {
    openModal(true, {
      isUpdate: false,
      existUniTypeIdList: existUniTypeIdList.value,
    });
  };

  const handleEdit = (record) => {
    openModal(true, {
      id: record.id,
      isUpdate: true,
      existUniTypeIdList: existUniTypeIdList.value,
    });
  };

  const handleDelete = (record) => {
    Modal.confirm({
      title: t('提示信息'),
      icon: createVNode(ExclamationCircleOutlined),
      content: t('是否确认删除？'),
      okText: t('确认'),
      cancelText: t('取消'),
      async onOk() {
        await deleteConversionList(record.id);
        notification.success({
          message: t('提示'),
          description: t('删除成功'),
        });
        reload();
      },
      onCancel() {},
    });
  };
</script>
<style lang="less" scoped>
  :deep(.vben-page-wrapper-content) {
    margin: 0;
  }

  :deep(.vben-basic-table-form-container) {
    margin-right: 0;
  }
</style>
